package com.frame.wechat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;


public class WechatUtil {
	
	public static String getSignToJS(Map<String,Object> map){
        ArrayList<String> list = new ArrayList<String>();
        for(Map.Entry<String,Object> entry:map.entrySet()){
            if(entry.getValue()!=""){
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String [] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < size; i ++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        Integer index = result.lastIndexOf("&");
        if(index!=-1) {
            result = result.substring(0, index);
        }
        result = new SHA1().getDigestOfString(result.getBytes()).toLowerCase();
        return result;
    }
	
	
	


}
